home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 September / macformat-004.iso / Shareware City / Graphics / VideoToolbox ƒ / VideoToolboxSources / MultipleChoice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-07  |  2.7 KB  |  89 lines  |  [TEXT/KAHL]

  1. /*
  2. MultipleChoice.c
  3. Accepts (and echoes on the console) a partial typed response to a
  4. multiple-choice question, expanding the response to print the full answer that
  5. it uniquely specifies. If alternative answers begin with the same letters (e.g.
  6. "Arabic" and "Armenian") then MultipleChoice keeps accepting characters until an
  7. answer is uniquely determined. Typing any non-printing character (e.g. Return or
  8. Enter) terminates the typed string, e.g. to distinguish "ant" from "anteater".
  9. If the typed string doesn't match any of the supplied answer strings then
  10. MultipleChoice makes the defaultChoice. Thus the user can select the default
  11. answer by just hitting return. Here's an example:
  12.     static char bread[]="Bread",fish[]="Fish",*breadFish[]={bread,fish};
  13.     short choice=1;
  14.     
  15.     printf("Would you like to eat Bread or Fish?");
  16.     choice=MultipleChoice(choice,2,breadFish);
  17.     printf("\n");
  18.     
  19. YesOrNo() restricts the answers to "yes" and "no", with a Boolean argument
  20. specifying the default.
  21.     printf("Do you want to go to heaven?");
  22.     choice=YesOrNo(0);
  23.     printf("\n");
  24.  
  25. NOTE:
  26. The declarations above explicitly allocate space for the strings "Bread" and "Fish". 
  27. In a stand-alone application you could implicitly allocate that space,
  28.     char *breadFish[]={"Bread","Fish"};
  29. but the THINK C compiler doesn't allow this in code resources (e.g. a MATLAB MEX file).
  30.  
  31. HISTORY:
  32. 2/16/93    dgp    wrote YesOrNo().
  33. 5/24/93    dgp    added MultipleChoice().
  34. 9/24/93    dgp    cosmetic.
  35. 1/25/94 dgp    enhanced MultipleChoice() to keep accepting characters until they uniquely
  36.             specify an answer, as suggested by Bart Farell.
  37. */
  38. #include "VideoToolbox.h"
  39. #if !defined(__TYPES__)
  40.     typedef unsigned char Boolean;
  41. #endif
  42.  
  43. Boolean YesOrNo(Boolean defaultChoice)
  44. /* Accept "y" or "n" and spell out "Yes" or "No". Anything else gets defaultChoice. */
  45. {
  46.     static char no[]="No",yes[]="Yes",*noYes[]={no,yes};
  47.     return MultipleChoice(defaultChoice,2,noYes);
  48. }
  49.  
  50. int MultipleChoice(short defaultChoice,short n,char *answer[])
  51. {
  52.     char c,s[64];
  53.     short i,j,imatch,match,matches,is;
  54.     
  55.     printf(" (%s):",answer[defaultChoice]);
  56.     fflush(stdout);
  57.     for(is=0;is<sizeof(s)-1;is++){
  58.         matches=0;
  59.         do{
  60.             c=getcharUnbuffered();
  61.         }while(c==-1);
  62.         if(c==14)abort();            /* Crude test for command-period. */
  63.         if(!isprint(c))c=0;
  64.         s[is]=c;
  65.         s[is+1]=0;
  66.         for(i=0;i<n;i++){
  67.             match=1;
  68.             for(j=0;j<=is;j++)match&=(tolower(s[j])==tolower(answer[i][j]));
  69.             if(match){
  70.                 imatch=i;
  71.                 matches++;
  72.             }
  73.         }
  74.         if(matches<=1 || s[is]==0)break;
  75.         printf("%c",c);
  76.         fflush(stdout);
  77.     }
  78.     
  79.     /* Erase partial answer, which may be longer than the default answer. */
  80.     is=strlen(s);
  81.     for(j=0;j<is;j++)printf("\b \b");
  82.     
  83.     /* Print answer */
  84.     if(matches==0)imatch=defaultChoice;
  85.     printf("%s.",answer[imatch]);
  86.     fflush(stdout);
  87.     return imatch;
  88. }
  89.